package com.lukebang.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lukebang.dao.CityCostRuleDAO;
import com.lukebang.dao.MerchantDAO;
import com.lukebang.dao.MerchantsWalletRecordDAO;
import com.lukebang.dao.RepairIndentDAO;
import com.lukebang.dao.WorkerDao;
import com.lukebang.dao.WorkerWalletRecordDao;
import com.lukebang.pojo.Result;
import com.lukebang.service.IRepairIndentService;

/**
 * 
 * RepairIndentForController 上午10:53:29
 * 
 * Copyright zhaocj Inc. All rights reserved. Love ME Like Justin Bieber.
 */
@Controller
@RequestMapping("/indent")
public class RepairIndentForController {
	private static Logger logger = LogManager.getLogger(RepairIndentForController.class.getName());
	@Resource
	@Qualifier("repairIndentSER")
	private IRepairIndentService repairIndentSER;
	@Resource
	private MerchantDAO  merchantDAO;
	@Resource
	private MerchantsWalletRecordDAO merchantsWalletRecordDAO;
	@Resource
	private WorkerDao workerDao;
	@Resource
	private WorkerWalletRecordDao workerWalletRecordDao;
	@Resource
	private CityCostRuleDAO cityCostRuleDAO;
	@Resource
	private RepairIndentDAO repairIndentDAO;

//	/**
//	 * 功能：取消订单
//	 * @param request
//	 * @param mesureIndentId
//	 * @return
//	 */
//	@RequestMapping("/quitRepairIndent")
//	@ResponseBody
//	public Map<String,Object> quitRepairIndent(HttpServletRequest request,HttpSession session,Integer repairIndentId){	
//		System.out.println("===========进入维修订单========");
//		return repairIndentSER.quitIndent(request, session, 2, repairIndentId);
//	}
//	
	// 添加维修订单
	@RequestMapping("/addRepair")
	public String repair(Model model) {
		model.addAttribute("oneMenu", "newTask");
		model.addAttribute("twoMenu", "repair");
		return "/cms/newTask/addRepair";
	}

	// 查看我的维修订单
	@RequestMapping("/repairList/{status}")
	public String RepairList(Model model, @PathVariable("status") String status) {
		logger.debug("page的状态是：{}", status);
		model.addAttribute("oneMenu", "taskList");
		model.addAttribute("twoMenu", "repairList");
		if ("pay".equals(status)) {
			return "/cms/taskList/repairList0";
		} else if ("taking".equals(status)) {
			System.out.println("维修》状态----------"+status);
			return "/cms/taskList/repairList1";
		} else if ("order".equals(status)) {
			return "/cms/taskList/repairList2";
		} else if ("move".equals(status)) {
			return "/cms/taskList/repairList3";
		} else {
			return "/cms/taskList/repairList4";
		}
	}

	@RequestMapping("/queryRepairIndentByMerchant")
	public @ResponseBody Map<String, Object> queryMeasureIndentByMerchant(HttpSession session, String cuName,
			String cuPhone, String indentID, String subscribeTimeBegin, String subscribeTimeEnd, String beginTimeBegin,
			String beginTimeEnd, Integer indentStatus, Integer offset, Integer limit, String sort, String order) {
		logger.debug("查询参数是，顾客姓名：{}，顾客手机号：{}，订单号：{}，预约时间开始：{}，预约时间结束：{}，下单时间开始：{}。下单时间结束：{}，订单的状态：{}", cuName, cuPhone,
				indentID, subscribeTimeBegin, subscribeTimeEnd, beginTimeBegin, beginTimeEnd, indentStatus);
		logger.debug("传入的表格参数是{},{},{},{}", offset, limit, sort, order);

		Map<String, Object> repairIndentMap = null;
		try {
			repairIndentMap = repairIndentSER.selectRepairIndentByMerchant(session, cuName, cuPhone, indentID,
					subscribeTimeBegin, subscribeTimeEnd, beginTimeBegin, beginTimeEnd, indentStatus, offset, limit,
					sort, order);
			System.out.println("维修订单号="+indentID+",status======"+indentStatus);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return repairIndentMap;
	}

	@RequestMapping("/makeRepairIndent")
	public @ResponseBody Result makeRepairIndent(HttpServletRequest request, String phone, String name, String weiXbody,
			String repairAddress, String province, String city, String country, String subscribeTime,
			Integer isNeedTakeGoods, String pickupPhone, String pickupProvince, String pickupCity, String pickupCountry,
			String pickAddress, HttpSession session) {
		Result result = new Result();
		try {
			Integer indentID = repairIndentSER.insertRepairIndent(request, phone, name, weiXbody,
					repairAddress, province, city, country, subscribeTime, isNeedTakeGoods, pickupPhone, pickupProvince,
					pickupCity, pickupCountry, pickAddress, session);
			result.setStatus(0);
			result.setData(indentID);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(1);
		}
		return result;
	}

	/**
	 * 
	 * @param indentID
	 *            根据订单的ID进行系统的支付
	 * @return
	 */
//	@RequestMapping("/payRepairIndent")
//	public String payRepairIndent(HttpServletRequest request, Integer indentID, Double addPrice, Integer relievedBao,
//			Integer crampetNumber, Integer fourClawNumber, Integer motorNumber, HttpSession session) {
//		logger.debug("需要付款的订单ID是：{}，加价费用是：{}，安心保选择：{}", indentID, addPrice, relievedBao);
//		try {
//			System.out.println("进入维修支付页面--------------");
//			System.out.println("订单ID是："+ indentID+",addPrice="+addPrice+",relievedBao="+ relievedBao+",crampetNumber="+crampetNumber+",fourClawNumber="+fourClawNumber+",motorNumber="+motorNumber);
//			repairIndentSER.modifyForPayRepairIndent(request, indentID, addPrice, relievedBao, crampetNumber,
//					fourClawNumber, motorNumber, session);
//			return "forward:/cms/alipay/alipayapi.jsp";
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return null;
//	}
}
